import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

public class GenCubeMatrix {
	private ArrayList<String> daysTable = new ArrayList<String>();
	private ArrayList<String> timesTable = new ArrayList<String>();
	private ArrayList<String> sourceTable = new ArrayList<String>();
	private ArrayList<LinkCharateristic> linkObject = new ArrayList<LinkCharateristic>();
	private File f;
	private File f2;
	private ReadFile read;
	private ArrayList<String> sourceIDArr;
	private ArrayList<String> sourceIDArrUnique;
	private ArrayList<String> linkIDArrUnique;
	private TimePeriodGrouping t = new TimePeriodGrouping();
	private double cube[][][];

	public GenCubeMatrix(ArrayList<LinkCharateristic> linkObject)
			throws IOException {
		this.linkObject = linkObject;

		DateTimeTable dtt = new DateTimeTable();
		setDaysTable(dtt.SetUpDaysTable());
		setTimesTable(dtt.SetUpTimeTable());
		Util.writeLog("\n3. Generate times(00:00:00 - 24:00:00) and days(Mon.-Fri.)");
		filterUniqueSourceID(linkObject);
	}

	// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	// Get the unique source id that were used
	public void filterUniqueSourceID(ArrayList<LinkCharateristic> linkObject)
			throws IOException {

		f = new File(Config.SOURCEID_PATH);
		f2 = new File(Config.LINKID_PATH);

		Util.writeLog("\t - Check a file " + Config.SOURCEID_PATH
				+ " is exist or not");

		if (!f.exists()) {
			Util.writeLog("\t - It is not exist");
			getUniqueSourceID(linkObject, Config.SOURCEID_PATH);
		} else if (f.exists() && f.length() > 0) {
			Util.writeLog("\t - It is exist");
			read = new ReadFile(Config.SOURCEID_PATH);
			sourceIDArrUnique = read.readText();
		}

		Util.writeLog("\t - Check a file " + Config.LINKID_PATH
				+ " is exist or not");
		if (!f2.exists()) {
			Util.writeLog("\t - It is not exist. Please generate file "
					+ Config.LINKID_PATH + " first.");
		} else if (f2.exists() && f2.length() > 0) {
			Util.writeLog("\t - It is exist");
			read = new ReadFile(Config.LINKID_PATH);
			linkIDArrUnique = read.readText();
			setLinkIDArrUnique(linkIDArrUnique);
		}

	}

	// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	public void getUniqueSourceID(ArrayList<LinkCharateristic> pntList,
			String output_filename) throws IOException {
		sourceIDArr = new ArrayList<String>();
		sourceIDArrUnique = new ArrayList<String>();
		linkIDArrUnique = new ArrayList<String>();
		for (LinkCharateristic linkCharateristic : pntList) {
			for (ClassifyTimePerDay class_time : linkCharateristic
					.getTimeInterval()) {
				// System.out.println(class_time.getSource_id());
				sourceIDArr.add(class_time.getSource_id());
			}
		}

		Collections.sort(sourceIDArr);
		// System.out.println(t.GetUniqueValues(sourceIDArr).size());
		for (String source_id : t.GetUniqueValues(sourceIDArr)) {
			// System.out.println("source_id.length() "+source_id.length());
			if (source_id.length() > 0) {
				sourceIDArrUnique.add(source_id.toString());
			}

		}
		Collections.sort(sourceIDArrUnique);

		// Write to a text file
		t.WriteProcess(sourceIDArrUnique, output_filename);
	}

	// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	public double[][][] setSizeArray() throws IOException {
		// [day][time][link_id]

		cube = new double[getDaysTable().size()][getTimesTable().size()][linkIDArrUnique
				.size()];

		for (int i = 0; i < cube.length; i++) {
			for (int j = 0; j < cube[i].length; j++) {
				for (int k = 0; k < cube[i][j].length; k++) {
					cube[i][j][k] = 0;
				}
			}
		}
		Util.writeLog("3.1 Completely Generated cube with 3D arrays");
		return cube;

	}

	// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	public ArrayList<String> getDaysTable() {
		return daysTable;
	}

	public void setDaysTable(ArrayList<String> daysTable) {
		this.daysTable = daysTable;
	}

	public ArrayList<String> getTimesTable() {
		return timesTable;
	}

	public void setTimesTable(ArrayList<String> timesTable) {
		this.timesTable = timesTable;
	}

	public ArrayList<String> getSourceTable() {
		return sourceTable;
	}

	public void setSourceTable(ArrayList<String> sourceTable) {
		this.sourceTable = sourceTable;
	}

	public ArrayList<String> getLinkIDArrUnique() {
		return linkIDArrUnique;
	}

	public void setLinkIDArrUnique(ArrayList<String> linkIDArrUnique) {
		this.linkIDArrUnique = linkIDArrUnique;
	}

}
